import store from '@/store'

/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkPermi(value) {
  if (value && value instanceof Array && value.length > 0) {
    const permissions = store.getters && store.getters.permissions
    const permissionDatas = value
    const all_permission = "*:*:*"

    const hasPermission = permissions.some(permission => {
      return all_permission === permission || permissionDatas.includes(permission)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
    return false
  }
}

/**
 * 角色权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkRole(value) {
  if (value && value instanceof Array && value.length > 0) {
    const roles = store.getters && store.getters.roles
    const permissionRoles = value
    const super_admin = "admin"

    const hasRole = roles.some(role => {
      return super_admin === role || permissionRoles.includes(role)
    })

    if (!hasRole) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkRole="['admin','editor']"`)
    return false
  }
}

/**
 * 是否启用
 * @returns {Boolean}
 */
export function isEnable() {
	let result = false;
	result = store.getters.consignor && store.getters.consignor.state == 0
	return result
}

/**
 * 是否认证
 * @param {number} value 校验值
 * @returns {Boolean}
 */
export function isCert(value) {
	let result = false;
	result = store.getters.consignor && store.getters.consignor.isCert == 1
	return result
}

/**
 * 是否认证失败
 * @param {number} value 校验值
 * @returns {Boolean}
 */
export function isReject(value) {
	let result = false;
	result = store.getters.consignor && store.getters.consignor.isCert == 2
	return result
}



export function gettime(type) {
	var nowdate = new Date();
	var year = nowdate.getFullYear(),
			month = nowdate.getMonth() + 1,
			date = nowdate.getDate(),
			day = nowdate.getDay(),
			week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
			h = nowdate.getHours(),
			m = nowdate.getMinutes(),
			s = nowdate.getSeconds(),
			h = checkTime(h),
			m = checkTime(m),
			s = checkTime(s);
	// return year + "年" + month + "月" + date + "日" + week[day] + " " + h +":" + m + ":" + s;
	return h +":" + m + ":" + s;
}